<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-form-item label="用户名" prop="username">
      <el-input
          v-model="queryParams.username"
          clearable
          placeholder="请输入用户名"
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="登录时间" prop="dateRange">
      <el-date-picker
          v-model="queryParams.dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          style="width: 200px;"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <div class="inner-container">
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column label="序号" width="80">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * (queryParams.pageSize) + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名"/>
      <el-table-column prop="ip" label="登录IP"/>
      <el-table-column prop="location" label="登录地点"/>
      <el-table-column prop="browser" label="浏览器"/>
      <el-table-column prop="os" label="操作系统" show-overflow-tooltip/>
      <el-table-column prop="status" label="登录状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="success">成功</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="msg" label="操作消息"/>
      <el-table-column prop="loginTime" label="登录时间" show-overflow-tooltip/>
    </el-table>

    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import request from '@/utils/request'

// --------------------------------------------------
const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])

const listData = () => {
  loading.value = true
  let data = {...queryParams.value}
  if (data.dateRange) {
    data.startTime = data.dateRange[0]
    data.endTime = data.dateRange[1]
    delete data.dateRange
  }
  request.get('/api/sys/loginLogs', {
    params: data
  }).then(res => {
    total.value = res.data.total;
    tableData.value = res.data.list;
    loading.value = false
  })
}

const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

// --------------------------------------------------
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
